package com.quanyan.place.wrapper.impl;

import com.alibaba.druid.util.StringUtils;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.TbBizPlace;
import com.quanyan.place.entity.TbBizPlaceExample;
import com.quanyan.place.entity.TbCity;
import com.quanyan.place.entity.apireq.ReqFinanceMeasureInfo;
import com.quanyan.place.entity.apireq.ReqPlaceRevenueStreamVo;
import com.quanyan.place.entity.apiresp.RespFinanceMeasureInfo;
import com.quanyan.place.entity.apiresp.RespPlaceRevenueStreamVo;
import com.quanyan.place.service.FinanceMeasureService;
import com.quanyan.place.service.PlaceBaseService;
import com.quanyan.place.service.PlaceInnerService;
import com.quanyan.place.wrapper.FinanceMeasureWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static java.lang.Integer.parseInt;

/**
 * Created by liuyaoxin on 16-7-26.
 */
@Service("financeMeasureWrapperImpl")
public class FinanceMeasureWrapperImpl implements FinanceMeasureWrapper {

    private Logger logger = LoggerFactory.getLogger(FinanceMeasureWrapperImpl.class);
    @Autowired
    FinanceMeasureService financeMeasureService;

    @Autowired
    PlaceInnerService placeInnerService;

    @Autowired
    PlaceBaseService placeBaseService;

    /**
     * 盈亏列表
     * @param reqFinanceMeasureInfo
     * @return
     */
    @Override
    public APIResponse<?> queryFinanceDataByCategoryView(ReqFinanceMeasureInfo reqFinanceMeasureInfo) {
        PageObj<List<RespFinanceMeasureInfo>> list = financeMeasureService.queryFinanceDataByCategoryView(reqFinanceMeasureInfo);
        if (list != null && list.getItems() != null) {
            for (RespFinanceMeasureInfo vo : list.getItems()) {
                TbCity cityVo=placeInnerService.getCityDataById(vo.getCityId());
                vo.setCityName(cityVo.getName());
                vo.setStartTime(reqFinanceMeasureInfo.getStartTime());
                vo.setEndTime(reqFinanceMeasureInfo.getEndTime());
                 if (PlaceConstants.PLACE_CATEGORY_ONE.equals(vo.getCategoryId())){
                     vo.setCategory("篮球");
                 }else if(PlaceConstants.PLACE_CATEGORY_TWO.equals(vo.getCategoryId())){
                     vo.setCategory("羽毛球");
                 }else if (PlaceConstants.PLACE_CATEGORY_THREE.equals(vo.getCategoryId())){
                     vo.setCategory("足球");
                 }else if (PlaceConstants.PLACE_CATEGORY_FOUR.equals(vo.getCategoryId())){
                     vo.setCategory("网球");
                 }else{
                     vo.setCategory(null);
                 }
            }
        }
        return APIResponse.returnSuccess(list);
    }

    /**
     * 盈亏列表导出
     * @param reqFinanceMeasureInfo
     * @return
     */
    @Override
    public List<RespFinanceMeasureInfo> queryFinanceDataByCategoryViewReport(ReqFinanceMeasureInfo reqFinanceMeasureInfo) {
        List<RespFinanceMeasureInfo> list = financeMeasureService.queryFinanceDataByCategoryViewReport(reqFinanceMeasureInfo);
        int i=0;
        int cityId=0;
        BigDecimal shouldPay=new BigDecimal("0.00");
        BigDecimal actuallyPay=new BigDecimal("0.00");
        BigDecimal totalShouldPay=new BigDecimal("0.00");
        BigDecimal totalActuallyPay=new BigDecimal("0.00");
       if (list!=null&&list.size()>0) {
           SimpleDateFormat smf=new SimpleDateFormat("yyyy-MM-dd");
           for (i=0;i<list.size();i++) {
               TbCity cityVo=placeInnerService.getCityDataById(list.get(i).getCityId());
               list.get(i).setCityName(cityVo.getName());
               list.get(i).setsTime(smf.format(reqFinanceMeasureInfo.getStartTime()));
               list.get(i).seteTime(smf.format(reqFinanceMeasureInfo.getEndTime()));
               if (PlaceConstants.PLACE_CATEGORY_ONE.equals(list.get(i).getCategoryId())){
                   list.get(i).setCategory("篮球");
               }else if(PlaceConstants.PLACE_CATEGORY_TWO.equals(list.get(i).getCategoryId())){
                   list.get(i).setCategory("羽毛球");
               }else if (PlaceConstants.PLACE_CATEGORY_THREE.equals(list.get(i).getCategoryId())){
                   list.get(i).setCategory("足球");
               }else if (PlaceConstants.PLACE_CATEGORY_FOUR.equals(list.get(i).getCategoryId())){
                   list.get(i).setCategory("网球");
               }else{
                   list.get(i).setCategory(null);
               }
               if (i==0){
                   cityId=list.get(i).getCityId();
               }
               if(list.get(i).getCityId().equals(cityId)) {
                   shouldPay = shouldPay.add(list.get(i).getShouldPay()==null?new BigDecimal("0.00"):list.get(i).getShouldPay());
                   actuallyPay=actuallyPay.add(list.get(i).getActuallyPay()==null?new BigDecimal("0.00"):list.get(i).getActuallyPay());
                   cityId=list.get(i).getCityId();
               }else{
                   RespFinanceMeasureInfo respFinanceMeasureInfo=new RespFinanceMeasureInfo();
                   respFinanceMeasureInfo.setCityId(cityId);
                   TbCity city=placeInnerService.getCityDataById(cityId);
                   respFinanceMeasureInfo.setCityName(city.getName());
                   cityId=list.get(i).getCityId();
                   respFinanceMeasureInfo.setsTime("");
                   respFinanceMeasureInfo.seteTime("");
                   respFinanceMeasureInfo.setCategory(city.getName()+"汇总");
                   respFinanceMeasureInfo.setShouldPay(shouldPay);
                   totalShouldPay=totalShouldPay.add(shouldPay);
                   respFinanceMeasureInfo.setActuallyPay(actuallyPay);
                   totalActuallyPay=totalActuallyPay.add(actuallyPay);
                   list.add(i,respFinanceMeasureInfo);
                   shouldPay=new BigDecimal("0.00");
                   actuallyPay=new BigDecimal("0.00");
               }

           }
           int counts=list.size();
           if (i==counts){
               RespFinanceMeasureInfo respFinanceMeasureInfo=new RespFinanceMeasureInfo();
               respFinanceMeasureInfo.setCityId(cityId);
               TbCity cVo=placeInnerService.getCityDataById(cityId);
               respFinanceMeasureInfo.setCityName(cVo.getName());
               respFinanceMeasureInfo.setsTime("");
               respFinanceMeasureInfo.seteTime("");
               respFinanceMeasureInfo.setCategory(cVo.getName()+"汇总");
               respFinanceMeasureInfo.setShouldPay(shouldPay);
               totalShouldPay=totalShouldPay.add(shouldPay);
               respFinanceMeasureInfo.setActuallyPay(actuallyPay);
               totalActuallyPay=totalActuallyPay.add(actuallyPay);
               list.add(i,respFinanceMeasureInfo);

               //总计
               RespFinanceMeasureInfo resp=new RespFinanceMeasureInfo();
               resp.setCityName("总计");
               resp.setsTime(smf.format(reqFinanceMeasureInfo.getStartTime()));
               resp.seteTime(smf.format(reqFinanceMeasureInfo.getEndTime()));
               resp.setCategory("总计");
               resp.setShouldPay(totalShouldPay);
               resp.setActuallyPay(totalActuallyPay);
               list.add(i+1,resp);
           }
       }
        return list;
    }

    /**
     * 分馆列表
     * @param reqFinanceMeasureInfo
     * @return
     */

    @Override
    public APIResponse<?> queryFinanceDataByPlaceView(ReqFinanceMeasureInfo reqFinanceMeasureInfo) {

        if (StringUtils.isEmpty(reqFinanceMeasureInfo.getName())) {
            reqFinanceMeasureInfo.setName(null);
        }else{
            reqFinanceMeasureInfo.setName("%"+reqFinanceMeasureInfo.getName().trim()+"%");
        }

        if(StringUtils.isEmpty(reqFinanceMeasureInfo.getCode())){
            reqFinanceMeasureInfo.setCode(null);
        }else{
            reqFinanceMeasureInfo.setCode(reqFinanceMeasureInfo.getCode().trim());
        }
        PageObj<List<RespFinanceMeasureInfo>> list = financeMeasureService.queryFinanceDataByPlaceView(reqFinanceMeasureInfo);
        if (list != null && list.getItems() != null) {
            for (RespFinanceMeasureInfo vo : list.getItems()) {
                TbCity cityVo=placeInnerService.getCityDataById(vo.getCityId());
                vo.setCityName(cityVo.getName());
                vo.setStartTime(reqFinanceMeasureInfo.getStartTime());
                vo.setEndTime(reqFinanceMeasureInfo.getEndTime());
                TbBizPlace placeVo= placeBaseService.getPlaceById(vo.getPlaceId());
                if(placeVo!=null){
                    vo.setName(placeVo.getName());
                }
            }
        }
        return APIResponse.returnSuccess(list);
    }

    /**
     * 分馆列表导出
     * @param reqFinanceMeasureInfo
     * @return
     */
    @Override
    public List<RespFinanceMeasureInfo> queryFinanceDataByPlaceViewReport(ReqFinanceMeasureInfo reqFinanceMeasureInfo) {
        if (StringUtils.isEmpty(reqFinanceMeasureInfo.getName())) {
            reqFinanceMeasureInfo.setName(null);
        }else{
            reqFinanceMeasureInfo.setName("%"+reqFinanceMeasureInfo.getName().trim()+"%");
        }
        if(StringUtils.isEmpty(reqFinanceMeasureInfo.getCode())){
            reqFinanceMeasureInfo.setCode(null);
        }else{
            reqFinanceMeasureInfo.setCode(reqFinanceMeasureInfo.getCode().trim());
        }
        List<RespFinanceMeasureInfo> list = financeMeasureService.queryFinanceDataByPlaceViewReport(reqFinanceMeasureInfo);
        int i=0;
        int cityId=0;
        BigDecimal shouldPay=new BigDecimal("0.00");
        BigDecimal actuallyPay=new BigDecimal("0.00");
        BigDecimal totalShouldPay=new BigDecimal("0.00");
        BigDecimal totalActuallyPay=new BigDecimal("0.00");
        if (list!=null&&list.size()>0) {
            SimpleDateFormat  smf= new SimpleDateFormat("yyyy-MM-dd");
            for (i=0;i<list.size();i++) {
                TbBizPlace pVo= placeBaseService.getPlaceById(list.get(i).getPlaceId());
                if(pVo!=null){
                    list.get(i).setName(pVo.getName());
                }
                TbCity cityVo=placeInnerService.getCityDataById(list.get(i).getCityId());
                list.get(i).setCityName(cityVo.getName());
                list.get(i).setsTime(smf.format(reqFinanceMeasureInfo.getStartTime()));
                list.get(i).seteTime(smf.format(reqFinanceMeasureInfo.getEndTime()));
                TbBizPlace placeVo= placeBaseService.getPlaceById(list.get(i).getPlaceId());
                if(placeVo!=null){
                list.get(i).setName(placeVo.getName());
                }
                if (i==0){
                    cityId=list.get(i).getCityId();
                }
                if(list.get(i).getCityId().equals(cityId)) {
                    shouldPay = shouldPay.add(list.get(i).getShouldPay()==null?new BigDecimal("0.00"):list.get(i).getShouldPay());
                    actuallyPay=actuallyPay.add(list.get(i).getActuallyPay()==null?new BigDecimal("0.00"):list.get(i).getActuallyPay());
                    cityId=list.get(i).getCityId();
                }else{
                    RespFinanceMeasureInfo respFinanceMeasureInfo=new RespFinanceMeasureInfo();
                    respFinanceMeasureInfo.setCityId(cityId);
                    TbCity city=placeInnerService.getCityDataById(cityId);
                    respFinanceMeasureInfo.setCityName(city.getName());
                    cityId=list.get(i).getCityId();
                    respFinanceMeasureInfo.setsTime("");
                    respFinanceMeasureInfo.seteTime("");
                    respFinanceMeasureInfo.setName(city.getName()+"汇总");
                    totalShouldPay=totalShouldPay.add(shouldPay);
                    respFinanceMeasureInfo.setShouldPay(shouldPay);
                    respFinanceMeasureInfo.setActuallyPay(actuallyPay);
                    totalActuallyPay=totalActuallyPay.add(actuallyPay);
                    list.add(i,respFinanceMeasureInfo);
                    shouldPay=new BigDecimal("0.00");
                    actuallyPay=new BigDecimal("0.00");
                }

            }
            int counts=list.size();
            if (i==counts){
                RespFinanceMeasureInfo respFinanceMeasureInfo=new RespFinanceMeasureInfo();
                respFinanceMeasureInfo.setCityId(cityId);
                TbCity city=placeInnerService.getCityDataById(cityId);
                respFinanceMeasureInfo.setCityName(city.getName());
                respFinanceMeasureInfo.setsTime("");
                respFinanceMeasureInfo.seteTime("");
                respFinanceMeasureInfo.setName(city.getName()+"汇总");
                respFinanceMeasureInfo.setShouldPay(shouldPay);
                totalShouldPay=totalShouldPay.add(shouldPay);
                respFinanceMeasureInfo.setActuallyPay(actuallyPay);
                totalActuallyPay=totalActuallyPay.add(actuallyPay);
                list.add(i,respFinanceMeasureInfo);

                //总计
                RespFinanceMeasureInfo resp=new RespFinanceMeasureInfo();
                resp.setCityName("总计");
                resp.setsTime(smf.format(reqFinanceMeasureInfo.getStartTime()));
                resp.seteTime(smf.format(reqFinanceMeasureInfo.getEndTime()));
                resp.setName("总计");
                resp.setShouldPay(totalShouldPay);
                resp.setActuallyPay(totalActuallyPay);
                list.add(i+1,resp);
            }
        }
        return list;
    }

    @Override
    public PageObj<List<RespPlaceRevenueStreamVo>> getPlaceRevenueStream(ReqPlaceRevenueStreamVo reqPlaceRevenueStreamVo) {
        return financeMeasureService.getPlaceRevenueStream(reqPlaceRevenueStreamVo);
    }

    @Override
    public List<RespPlaceRevenueStreamVo> getPlaceRevenueStreamExport(ReqPlaceRevenueStreamVo reqPlaceRevenueStreamVo) {
        return financeMeasureService.getPlaceRevenueStreamExport(reqPlaceRevenueStreamVo);
    }


    @Override
    public void importFinanceDataByCategory(ReqFinanceMeasureInfo reqFinanceMeasureInfo) {
        financeMeasureService.importFinanceDataByCategory(reqFinanceMeasureInfo);
    }

}

